package com.dragon.mmochat.model.scheduleJob;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;

import com.dragon.mmochat.model.activity.OnlineAwardInfo;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.service.MmochatChatService;
import com.dragon.mmochat.service.MmochatDao;
import com.dragon.mmochat.service.MmochatGmService;
import com.dragon.mmochat.service.MmochatMainService;
import com.dragon.mmochat.util.MmochatUtil;

public class OnlineAwardActivityJob implements Job {
	@Override
	public void execute(JobExecutionContext context) {
		try {
			OnlineAwardInfo awardInfo = (OnlineAwardInfo) context
					.getJobDetail().getJobDataMap().get("data");
			if (awardInfo != null) {
				List<MmochatPlayer> onlineList = new ArrayList<MmochatPlayer>(
						MmochatMainService.players.values());
				if (onlineList.size() > 0) {
					int index = MmochatUtil.getRandomValue(onlineList.size());
					MmochatPlayer me = onlineList.get(index);
					if (me != null) {
						try {
							MmochatDao.addNewActivityRecord2(me.getSkyId(), me
									.getRoleId(), awardInfo.getMoneyCount(),
									awardInfo.getId(), me.getName());
						} catch (Exception e) {
							e.printStackTrace();
							return;
						}

						String moneyStr = MmochatUtil.wrapColor(awardInfo
								.getMoneyCount()
								+ awardInfo.getMoneyType().toString(),
								Color.yellow);
						String roleStr = MmochatUtil.wrapColor(me.getName(),
								Color.green);

						// 发送系统公告
						String sysInfo = awardInfo.getSysMsg();
						if (sysInfo != null) {
							sysInfo = sysInfo.replace("[roleName]", roleStr);
							sysInfo = sysInfo.replace("[money]", moneyStr);
							MmochatChatService.sendSystemMsg(sysInfo);
						}

						// 发送客服消息
						String userInfo = awardInfo.getUserMsg();
						if (userInfo != null) {
							userInfo = userInfo.replace("[roleName]",
									roleStr);
							userInfo = userInfo.replace("[money]", moneyStr);
							MmochatGmService.sendKufuPrivateMsg(me.getRoleId(),
									userInfo, false);
						}

						// 如果是金钱或元宝，则马上下发奖励(不能大于10000000，防止数据异常)
						int moneyCount = awardInfo.getMoneyCount();
						if (moneyCount > 0 && moneyCount < 10000000) {
							switch (awardInfo.getMoneyType()) {
							case 元宝: {
								me.addBig_money(moneyCount);
								try {
									MmochatDao.addRoleBigMoney(me.getRoleId(),
											moneyCount);
								} catch (Exception e) {
									e.printStackTrace();
								}
								break;
							}
							case 金钱: {
								me.addAndUpdateSmall_money(moneyCount);
								try {
									MmochatDao.addRoleSmallMoney(
											me.getRoleId(), moneyCount);
								} catch (Exception e) {
									e.printStackTrace();
								}
								break;
							}
							default:
								break;
							}
						}
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}